﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopNet.Util;

namespace ShopNet
{
	public partial class Register : System.Web.UI.Page
	{
        public new Shop Master
        {
            get
            {
                return ((Shop)(base.Master));
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.ExibirMensagem(Shop.TipoMensagem.Success, string.Empty);
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
            if (Page.IsPostBack)
            {
                Page.Validate();
                if (!Page.IsValid)
                {
                    TextBox UserName = RegisterUserWizardStep.FindControlRecursive("UserName") as TextBox;
                    TextBox Password = RegisterUserWizardStep.FindControlRecursive("Password") as TextBox;
                    TextBox Email = RegisterUserWizardStep.FindControlRecursive("Email") as TextBox;
                    TextBox ConfirmPassword = RegisterUserWizardStep.FindControlRecursive("ConfirmPassword") as TextBox;
                    if (string.IsNullOrWhiteSpace(UserName.Text))
                    {
                        UserName.SetCssClassError();
                    }
                    if (string.IsNullOrWhiteSpace(Password.Text))
                    {
                        Password.SetCssClassError();
                    }
                    if (string.IsNullOrWhiteSpace(Email.Text))
                    {
                        Email.SetCssClassError();
                    }
                    if (string.IsNullOrWhiteSpace(ConfirmPassword.Text))
                    {
                        ConfirmPassword.SetCssClassError();
                    }
                    if (ConfirmPassword.Text!=Password.Text)
                    {
                        ConfirmPassword.SetCssClassError();
                        Password.SetCssClassError();
                    }
                }
            }
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            //Adicionar na Role RegisterUser.UserName
            if (!Roles.IsUserInRole(RegisterUser.UserName, "Usuarios"))
            {
                Roles.AddUserToRole(RegisterUser.UserName, "Usuarios");
            }

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }

	}
}